package com.cssnj.portal.service.user;

import com.cssnj.portal.domain.user.GlGnSyqkRepository;
import com.cssnj.portal.domain.user.GlYhGnPjEntity;
import com.cssnj.portal.domain.user.GlYhGnPjRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.domain.exception.BaseSysException;
import io.predictech.framework.service.base.BaseService;
import io.predictech.framework.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("evaluateService")
public class EvaluateService extends BaseService {

    @Autowired
    private GlYhGnPjRepository glYhGnPjRepository;

    @Override
    protected JsonResponse invokingService(JsonRequest jsonRequest) throws SQLException, BaseException {
        if ("init".equals(jsonRequest.getHandleCode())) {
            return init(jsonRequest);
        } else if ("queryPjLb".equals(jsonRequest.getHandleCode())) {
            return queryPjLb(jsonRequest);
        } else if ("savePj".equals(jsonRequest.getHandleCode())) {
            return savePj(jsonRequest);
        } else if ("queryLsPf".equals(jsonRequest.getHandleCode())) {
            return queryLsPf(jsonRequest);
        }
        return null;
    }

    //初始化
    private JsonResponse init(JsonRequest jsonRequest) throws SQLException {
        JsonResponse jsonResponse = new JsonResponse();
        String gnmc = "";
        String gnid = (String) jsonRequest.getReqData().get("gnid");
        String sql = "select a.gn_mc gnmc from gl_gn a where a.gn_id = ?";
        List<Object> params = new ArrayList<Object>();
        params.add(gnid);
        SqlRowSet rs = this.queryDao.queryForRowSet(sql, params);
        if (rs.next()) {
            gnmc = rs.getString("gnmc");
        }
        jsonResponse.getRepData().put("gnmc", gnmc);
        return jsonResponse;
    }

    //查询评价列表(分页查询)
    private JsonResponse queryPjLb(JsonRequest jsonRequest) throws SQLException, BaseException {
        SqlRowSet rs;
        JsonResponse jsonResponse = new JsonResponse();
        String sql = "";
        String total_sql = "";
        //获取请求参数
        String gnid = (String) jsonRequest.getReqData().get("gnid");
        String rows = (String) jsonRequest.getReqData().get("rows");
        String page = (String) jsonRequest.getReqData().get("page");
        if (null == rows || "".equals(rows)) {
            rows = "10";
        }
        if (null == page || "".equals(page)) {
            page = "1";
        }
        //查询语句
        sql = "select hc, swry_xm xm, pjdj_mc dj, pjnr nr, lrsj sj from (select rownum hc, swry_xm, pjdj_mc, pjnr, lrsj from (select a.swry_xm, b.pjdj_mc, a.pjnr, to_char(a.lr_sj, 'yyyy-mm-dd hh24:mi:ss') lrsj from Gl_Yh_Gn_Pj a, dm_gy_pjdj b where a.gn_id = ? and a.pjdj_dm = b.pjdj_dm(+) order by a.lr_sj desc)) d where hc >= ? * (? -1) + 1 and hc <= ? * ?";
        total_sql = "select count(1) as gs from Gl_Yh_Gn_Pj a where a.gn_id = ?";
        //查询参数
        List<Object> params = new ArrayList<Object>();
        params.add(gnid);
        params.add(rows);
        params.add(page);
        params.add(rows);
        params.add(page);
        List<Object> total_params = new ArrayList<Object>();
        total_params.add(gnid);
        rs = this.queryDao.queryForRowSet(sql, params);
        Map<String, Object> map;
        List<Map<String, Object>> jlList = new ArrayList<Map<String, Object>>();
        while (rs.next()) {
            map = new HashMap<String, Object>();
            map.put("hc", rs.getString("hc"));
            map.put("xm", rs.getString("xm"));
            map.put("dj", rs.getString("dj"));
            map.put("nr", rs.getString("nr"));
            map.put("sj", rs.getString("sj"));
            jlList.add(map);
        }
        map = this.queryDao.queryForMap(total_sql, total_params);
        String total = String.valueOf(map.get("gs"));
        try {
            ObjectMapper objectMapper = StringUtil.getBaseObjectMapper();
            HashMap<String, Object> r_map = new HashMap<String, Object>();
            r_map.put("total", total);
            r_map.put("rows", jlList);
            jsonResponse.getRepData().put("str", objectMapper.writeValueAsString(r_map));
        } catch (JsonProcessingException e) {
            throw new BaseSysException(e.getMessage());
        }
        return jsonResponse;
    }

    //
    //查询历史评分
    private JsonResponse queryLsPf(JsonRequest jsonRequest) throws SQLException, BaseException {
        JsonResponse jsonResponse = new JsonResponse();
        String sql = "";
        Map<String, Object> map;
        //获取请求参数
        String gnid = (String) jsonRequest.getReqData().get("gnid");
        //查询参数
        List<Object> params = new ArrayList<Object>();
        params.add("1");
        //查询代码表
        sql = "select a.pjdj_dm,a.pjdj_mc from dm_gy_pjdj a where a.xy_bj = ?";
        List<Map<String, Object>> djList = new ArrayList<Map<String, Object>>();
        SqlRowSet rs = this.queryDao.queryForRowSet(sql, params);
        while (rs.next()) {
            map = new HashMap<String, Object>();
            map.put("djdm", rs.getString("pjdj_dm"));
            map.put("djmc", rs.getString("pjdj_mc"));
            djList.add(map);
        }
        //查询登记次数
        List<Map<String, Object>> sjList = new ArrayList<Map<String, Object>>();
        sql = "select count(1) as gs from gl_yh_gn_pj a where a.gn_id = ? and a.pjdj_dm = ?";
        for (int i = 0; i < djList.size(); i++) {
            params = new ArrayList<Object>();
            params.add(gnid);
            params.add(djList.get(i).get("djdm"));
            rs = this.queryDao.queryForRowSet(sql, params);
            while (rs.next()) {
                map = new HashMap<String, Object>();
                map.put("gs", rs.getString("gs"));
                sjList.add(map);
            }
        }
        jsonResponse.getRepData().put("djList", djList);
        jsonResponse.getRepData().put("sjList", sjList);
        return jsonResponse;
    }

    //保存评价
    @Transactional
    public JsonResponse savePj(JsonRequest jsonRequest) throws SQLException {
        JsonResponse jsonResponse = new JsonResponse();
        //请求参数
        String gnid = (String) jsonRequest.getReqData().get("gnid");
        String wdpf = (String) jsonRequest.getReqData().get("wdpf");
        String wdpj = (String) jsonRequest.getReqData().get("wdpj");
        String swrydm = (String) jsonRequest.getReqData().get("swrydm");
        String swryxm = (String) jsonRequest.getReqData().get("swryxm");
        String swrysfdm = (String) jsonRequest.getReqData().get("swrysfdm");
        String swrysfmc = (String) jsonRequest.getReqData().get("swrysfmc");
        String swjgdm = (String) jsonRequest.getReqData().get("swjgdm");
        String swjgmc = (String) jsonRequest.getReqData().get("swjgmc");
        //获取当前时间
        Timestamp lrsj = this.queryDao.getSystimestamp();
        //删除之前的评论
        glYhGnPjRepository.deleteByPK(gnid, swrysfdm);
        //更新新的评论
        GlYhGnPjEntity vo = new GlYhGnPjEntity();
        vo.setGnId(gnid);
        vo.setLrryId(swrydm);
        vo.setLrSj(lrsj);
        vo.setPjdjDm(wdpf);
        vo.setPjnr(wdpj);
        vo.setSwryDm(swrydm);
        vo.setSwryXm(swryxm);
        vo.setSwrysfDm(swrysfdm);
        vo.setSwrysfMc(swrysfmc);
        vo.setSfswjgDm(swjgdm);
        vo.setSfswjgmc(swjgmc);
        glYhGnPjRepository.save(vo);
        return jsonResponse;
    }
}